package cn.ai.shoes.core.pojo.entity;

import cn.ai.shoes.core.annotation.AddGroup;

import cn.ai.shoes.core.annotation.UpdateGroup;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.fasterxml.jackson.annotation.JsonIgnore;
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.*;
import lombok.*;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.validation.annotation.Validated;

import java.time.LocalDateTime;
import java.util.Collection;
import java.util.Collections;

import static cn.ai.shoes.common.constant.GolbalConstants.UserConstants.ADMIN;
import static cn.ai.shoes.common.constant.ValidationGroups.ADD_GROUP;
import static cn.ai.shoes.common.constant.ValidationGroups.UPDATE_GROUP;

/**
 *
 * @TableName user
 */
@Data
@TableName(value ="user")
@Schema(name= "用户类")
@Validated
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class MyUser implements UserDetails{
    @TableId(type = IdType.AUTO)
    @Schema(description = "主键",accessMode = Schema.AccessMode.READ_ONLY)
    private Integer id;

    /**
     * 用户唯一标识
     */
    @Schema(description = "用户唯一标识",accessMode = Schema.AccessMode.READ_ONLY)
    private String uid;
    /**
     * 用户名
     */
    @Schema(description = "用户名")
    @NotBlank(message = "用户名不能为空")
    private String username;

    /**
     * 密码
     */
    @Schema(description = "密码")
    @Size(min = 1, max = 20,message = "密码最小1位，最长20位")
    @NotBlank(message = "密码不能为空")
    private String password;

    /**
     * 昵称
     */
    @Schema(description = "昵称")
    @NotBlank(message = "昵称不能为空")
    private String nickname;
    /**
     * 性别
     */
    @Schema(description = "性别,0:女,1:男,2:未知")
    private Integer sex = 2;

    @Schema(description = "头像")
    private String avatar;

    // /**
    //  * 邮箱
    //  */
    // @Schema(description = "邮箱")
    // // @Pattern(regexp = "^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\\.[a-zA-Z0-9_-]+)+$",message = "邮箱格式不正确",groups = {AddGroup.class, UpdateGroup.class})
    // private String email;

    /**
     * 手机号
     */
    @Schema(description = "手机号")
    @Pattern(regexp = "^1[3-9]\\d{9}$",message = "手机号格式不正确")
    @NotBlank(message = "手机号不能为空")
    private String phone;

    /**
     * 角色(访客、会员、管理员)
     */
    @Schema(description = "角色,VISITOR:访客,MEMBER:会员,ADMIN:管理员",accessMode = Schema.AccessMode.READ_ONLY )
    private String role;

    /**
     * 备注
     */
    @Schema(description = "备注",accessMode = Schema.AccessMode.READ_ONLY)
    private String remark;

    /**
     * 过期时间
     */
    @Schema(description = "过期时间",accessMode = Schema.AccessMode.READ_ONLY)
    private LocalDateTime expireTime;
    /**
     * 状态(0:禁用,1:正常)
     */
    @Schema(description = "状态,0:禁用,1:正常",accessMode = Schema.AccessMode.READ_ONLY)
    private boolean enabled;

    /**
     * 是否删除
     */
    @Schema(description = "是否删除,0:否,1:是",accessMode = Schema.AccessMode.READ_ONLY)
    private Integer deleted;

    /**
     * 更新时间
     */
    @Schema(description = "更新时间",accessMode = Schema.AccessMode.READ_ONLY)
    private LocalDateTime updateTime;

    /**
     * 创建时间
     */
    @Schema(description = "创建时间",accessMode = Schema.AccessMode.READ_ONLY)
    private LocalDateTime createTime;

    /**
     * 创建人
     */
    @Schema(description = "创建人",accessMode = Schema.AccessMode.READ_ONLY)
    private String creator;

    /**
     * 更新人
     */
    @Schema(description = "更新人",accessMode = Schema.AccessMode.READ_ONLY)
    private String updater;
    @Schema(description = "短信验证码")
    @TableField(exist = false)
    private String smsCode;

    @Override
    @JsonIgnore
    public Collection<? extends GrantedAuthority> getAuthorities() {
        return Collections.singleton(new SimpleGrantedAuthority("ROLE_" + this.role));
    }

    @Override
    public String getPassword() {
        return this.password;
    }

    @Override
    public String getUsername() {
        return this.username;
    }

    @Override
    // @JsonIgnore
    public boolean isAccountNonExpired() {
        // 根据 expireTime 判断账户是否过期
        return expireTime == null || expireTime.isAfter(LocalDateTime.now());
    }

    @Override
    // @JsonIgnore
    public boolean isAccountNonLocked() {
        // 根据 deleted 字段判断账户是否锁定（0表示未删除/未锁定）
        return deleted != null && deleted == 0;
    }

    @Override
    // @JsonIgnore
    public boolean isCredentialsNonExpired() {
        // 凭证永不过期（按需修改）
        return true;
    }

    @Override
    // @JsonIgnore
    public boolean isEnabled() {
        // 根据 enabled 字段判断账户启用状态
        return this.enabled;
    }

    public boolean isAdmin() {
        return ADMIN.equals(this.role);
    }
}
